package com.example.recuitactivity.enterprise_fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.example.recuitactivity.helper.DbOpenHelper;
import com.example.recuitactivity.EditRecuitInfoActivity;
import com.example.recuitactivity.R;
import com.example.recuitactivity.adapter.EnterpriseNeedInfoAdapter;
import com.example.recuitactivity.bean.RecuitInfo;

import java.util.ArrayList;
import java.util.List;

public class EnterpriseNeedinfoFragment extends Fragment {
    private View messageLayout;
    private ImageButton ib_add;
    private RecyclerView rv;
    private List<RecuitInfo> mDatas=new ArrayList<>();
    private EnterpriseNeedInfoAdapter adapter;
    private DbOpenHelper dbOpenHelper;

    private String username;

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch(view.getId()){
                case R.id.ib_add_recuit_info:
                    Intent intent=new Intent(getActivity(), EditRecuitInfoActivity.class);
                    intent.putExtra("username",username);
                    intent.setAction("createRecruitInfo");
                    startActivity(intent);
                    break;
            }
        }
    };

    public void initRecyclerView(){
        LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
        rv.setLayoutManager(layoutManager);
        adapter=new EnterpriseNeedInfoAdapter(getActivity(),mDatas);
        rv.setAdapter(adapter);
    }


    public void initUI() {
        dbOpenHelper=new DbOpenHelper(getActivity());
        ib_add=(ImageButton) messageLayout.findViewById(R.id.ib_add_recuit_info);
        rv=(RecyclerView) messageLayout.findViewById(R.id.rv_enterprise_need);

        ib_add.setOnClickListener(listener);
    }

    public void getDatas(){
        mDatas=dbOpenHelper.getOwnRecuitInfoByUsername(username);
    }

    public EnterpriseNeedinfoFragment(String username){
        this.username=username;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        messageLayout = inflater.inflate(R.layout.fragment_enterprise_needinfo, container, false);
        initUI();
        getDatas();
        initRecyclerView();
        return messageLayout;
    }

    @Override
    public void onResume() {
        super.onResume();
        getDatas();
        adapter.setmDatas(mDatas);
    }
}